package com.kerpow.KSheet;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class NumberEntry extends Dialog {

	public enum NumberEntryMode{
		Damage,
		Healing,
		TempHP,
		GainPowerPoints,
		LosePowerPoints
	}
	
	public interface OnEntryListener{
		public void OnEntry(int value, boolean addSurgeValue, boolean loseSurge);
	}

	private OnEntryListener _onEntryHandler;
	
	CheckBox _addsurgevalue;
	CheckBox _losesurge;
	
	EditText _textEntry;
	Context _context;
	
	NumberEntryMode _myMode;
	

	public NumberEntry(Context context, OnEntryListener onEntryHandler) {
		super(context);
		
		_context = context;
		_onEntryHandler = onEntryHandler;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.numberentry);
		
		
		
		_addsurgevalue = (CheckBox) findViewById(R.id.addsurge);
		_losesurge = (CheckBox) findViewById(R.id.losesurge);
		
		_textEntry = (EditText) findViewById(R.id.damage_amount);
		final Button b = (Button)findViewById(R.id.damage_b_done);
		
		_textEntry.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(keyCode == KeyEvent.KEYCODE_ENTER)
					b.performClick();
				return false;
			}
		});
		
		b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try
				{
					InputMethodManager imm=(InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0); 
					
					int inputVal=0;
					if(_textEntry.getText().length() != 0)
						inputVal = Integer.parseInt(_textEntry.getText().toString());	
					
					
					switch(_myMode){
					case Damage:
					case TempHP:
					case GainPowerPoints:
					case LosePowerPoints:
						_onEntryHandler.OnEntry(inputVal, false, false);
						break;
					case Healing:
						_onEntryHandler.OnEntry(inputVal, _addsurgevalue.isChecked(), _losesurge.isChecked());
						break;
					}
				}catch(Exception ex){
					
				}
				
				dismiss();
			}
		});
		
	}

	public void setup(NumberEntryMode mode){
		_myMode = mode;
		switch(_myMode){
		case Damage:
			setTitle(R.string.dlgtitle_damage);
			_addsurgevalue.setVisibility(View.GONE);
			_losesurge.setVisibility(View.GONE);
			break;
		case Healing:
			_addsurgevalue.setVisibility(View.VISIBLE);
			_losesurge.setVisibility(View.VISIBLE);
			_addsurgevalue.setChecked(false);
			_losesurge.setChecked(false);
			setTitle(R.string.dlgtitle_healing);
			break;
		case TempHP:
			_addsurgevalue.setVisibility(View.GONE);
			_losesurge.setVisibility(View.GONE);
			setTitle(R.string.dlgtitle_temphp);
			break;
		case GainPowerPoints:
		case LosePowerPoints:
			setTitle(R.string.dlgtitle_powerpoints);
			_addsurgevalue.setVisibility(View.GONE);
			_losesurge.setVisibility(View.GONE);
			break;
		}
		
		_textEntry.setText("");
	}
	
	@Override
	public void show() {
		
		InputMethodManager imm=(InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 
		super.show();
	}
	
}
